package com.hypo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hypo.pojo.NursingProject;
import com.hypo.pojo.NursingProjectQueryParam;
import com.hypo.pojo.PageResult;
import com.hypo.pojo.Result;
import com.hypo.service.NursingProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/nursing")
@RequiredArgsConstructor
public class NursingProjectController {
    private final NursingProjectService nursingProjectService;

    /**
     * 分页查询
     *
     * @param nursingProjectQueryParam 请求参数
     * @return 返回
     */
    @GetMapping
    public Result pageQuery(NursingProjectQueryParam nursingProjectQueryParam) {
        IPage<NursingProject> page = new Page<>(nursingProjectQueryParam.getPage(), nursingProjectQueryParam.getPageSize());
        LambdaQueryWrapper<NursingProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!Objects.isNull(nursingProjectQueryParam.getName()), NursingProject::getName, nursingProjectQueryParam.getName());
        IPage<NursingProject> pageInfo = nursingProjectService.page(page, queryWrapper);
        return Result.success(new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords()));
    }

    /**
     * 新增
     *
     * @param nursingProject 新增数据
     * @return 返回
     */
    @PostMapping
    public Result save(@RequestBody NursingProject nursingProject) {
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectService.save(nursingProject);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids 删除Id集合
     * @return 返回
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        nursingProjectService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 回显，根据Id查询信息
     *
     * @param id id
     * @return 返回
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        NursingProject nursingProject = nursingProjectService.getById(id);
        return Result.success(nursingProject);
    }

    /**
     * 修改
     *
     * @param nursingProject 修改数据
     * @return 返回
     */
    @PutMapping
    public Result update(@RequestBody NursingProject nursingProject) {
        nursingProjectService.updateById(nursingProject);
        return Result.success();
    }

    @GetMapping("/all")
    public Result queryAll() {
        return Result.success(nursingProjectService.list());
    }
}
